Imports System.Web
Imports MIMS.WRS.Entities

Public Class Utilities
    Public Const HashAlgorithm As String = "md5" '"sha1" 'vbNullString '

    Public Shared Function HashPassword(ByVal pwd As String) As String
        If Trim(HashAlgorithm) = vbNullString Then
            Return pwd
        Else
            Return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, HashAlgorithm)
        End If
    End Function


    Public Shared Function GetRandomFileName() As String
        Dim sFN As String

        sFN = sFN & Year(Now())
        sFN = sFN & Format(Month(Now()), "00")
        sFN = sFN & Format(Day(Now()), "00")
        sFN = sFN & Format(Hour(Now), "00")
        sFN = sFN & Format(Minute(Now()), "00")
        sFN = sFN & Format(Second(Now()), "00")

        GetRandomFileName = sFN
    End Function


    Public Shared ReadOnly Property NAText() As String
        Get
            Return "<span class=""highlight"">N/A</span>"
        End Get
    End Property

    Public Shared Function GetBOTypeMeaning(ByVal value As EBOType) As String
        Select Case value
            Case EBOType.Ordered
                Return "Ordered"
            Case EBOType.StockClear
                Return "Stock Clear"
            Case EBOType.StockFact
                Return "Stock Fact"
        End Select
    End Function

    'Public Shared Function GetBOPackTypeMeaning(ByVal value As String) As String
    '    Select Case value.ToUpper()
    '        Case "1PIE"
    '            Return "Pieces"
    '        Case "2SET"
    '            Return "Sets"
    '        Case "2UNT"
    '            Return "Units"
    '        Case "3OUT"
    '            Return "Outers"
    '        Case "4AMP"
    '            Return "Mixed Pallet"
    '        Case "4HDP"
    '            Return "HD Pallet"
    '        Case "5TSP"
    '            Return "TS Pallet"
    '        Case "6TDP"
    '            Return "TD Pallet"
    '        Case "7SDP"
    '            Return "SD Pallet"
    '        Case "8HCC"
    '            Return "HC Crate"
    '        Case Else
    '            Return value
    '    End Select
    'End Function

    'Public Shared Function GetBOPackType2Meaning(ByVal value As String) As String
    '    Select Case value.ToUpper()
    '        Case "1PIE"
    '            Return "Pieces"
    '        Case "2SET"
    '            Return "Sets"
    '        Case "2UNT"
    '            Return "Units"
    '        Case "3OUT"
    '            Return "Outers"
    '        Case "4AMP"
    '            Return "Mixed Pallet"
    '        Case "4HDP"
    '            Return "Half Pallet"
    '        Case "5TSP"
    '            Return "Tall Pallet"
    '        Case Else
    '            Return value
    '    End Select
    'End Function

    Public Shared Function GetBulkMeaning(ByVal value As String) As String
        Select Case value.ToUpper()
            Case "N"
                Return "New"
            Case "F"
                Return "Full"
            Case "P"
                Return "Part"
            Case Else
                Return value
        End Select
    End Function

    Public Shared Function GetPackMeaning(ByVal value As String) As String
        Select Case value.ToUpper()
            Case "H"
                Return "HD Pallet"
            Case "T"
                Return "TS Pallet"
            Case "P"
                Return "TD Pallet"
            Case "L"
                Return "Loose"
            Case "M"
                Return "Mixed"
            Case "S"
                Return "Samples"
            Case "D"
                Return "SD Pallet"
            Case "C"
                Return "HC Crate"
            Case Else
                Return value
        End Select
    End Function

    Public Shared Function GetTypeMeaning(ByVal value As String) As String
        Select Case value.ToUpper()
            Case "W"
                Return "Warehouse"
            Case "D"
                Return "Direct FOB"
            Case "H"
                Return "Direct HD"
            Case Else
                Return value
        End Select
    End Function

    Public Shared Function GetTypeMeaning(ByVal value As Object) As String
        If (value Is DBNull.Value) Then
            Return "&nbsp;"
        Else
            Return GetTypeMeaning(Convert.toString(value))
        End If
    End Function

    Public Shared Function GetPrimaryMeaning(ByVal value As Object) As String
        If (value Is DBNull.Value) Then
            Return "&nbsp;"
        Else
            Return GetPrimaryMeaning(Convert.toString(value))
        End If
    End Function

    Public Shared Function GetPrimaryMeaning(ByVal value As String) As String
        Select Case value.ToUpper()
            Case "P"
                Return "Primary"
            Case "S"
                Return "Secondary"
            Case "T"
                Return "Combined"
            Case "U"
                Return "B Combined"
            Case Else
                Return value
        End Select
    End Function

    Public Shared Function GetPaymentMeaning(ByVal value As String) As String
        Select Case value.ToUpper()
            Case "D"
                Return "Deposit Paid"
            Case "F"
                Return "Full Paid"
            Case "R"
                Return "L/C Raised"
            Case "P"
                Return "L/C Paid"
            Case "C"
                Return "Credit"
            Case Else
                Return value
        End Select
    End Function

    Public Shared Function GetWeek(ByVal inDate As Date) As Int32
        Dim wk As Int32 = DatePart(DateInterval.WeekOfYear, inDate.AddDays(-1))

        If wk > 52 Then
            wk = 52
        End If

        Return wk
    End Function

    Public Shared Function ReplaceUrlParameter(ByVal input As String, ByVal paramName As String, ByVal value As String) As String
        Dim idx As Integer
        Dim nextIdx As Integer
        idx = input.IndexOf(paramName + "=")

        If (idx > -1) Then
            nextIdx = input.IndexOf("&", idx)
            If (nextIdx > -1) Then
                Return String.Format("{0}&{1}={2}{3}", input.Substring(0, idx), paramName, value, input.Substring(nextIdx, input.Length - nextIdx))
            Else
                Return String.Format("{0}&{1}={2}", input.Substring(0, idx), paramName, value)
            End If
        Else
            Return String.Format("{0}&{1}={2}", input, paramName, value)
        End If
    End Function

    Public Shared Sub ShowMessagePage(ByVal msg As String)
        Throw New Exception(msg)
    End Sub

    Public Shared Function GetReferrerUrl(ByVal defaultUrl As String) As String
        If Not HttpContext.Current Is Nothing AndAlso Not HttpContext.Current.Request Is Nothing _
            AndAlso Not HttpContext.Current.Request.UrlReferrer Is Nothing _
            AndAlso HttpContext.Current.Request.Url.AbsolutePath <> HttpContext.Current.Request.UrlReferrer.AbsolutePath Then
            Return HttpContext.Current.Request.UrlReferrer.PathAndQuery
        Else
            Return defaultUrl
        End If
    End Function

    Public Shared Function GetIntText(ByVal data As Object) As String
        If data Is DBNull.Value OrElse Convert.ToInt32(data) = Nulls.NullInteger Then
            Return "&nbsp;"
        Else
            Return Convert.ToInt32(data).ToString()
        End If
    End Function

    Public Shared Function GetDecimalText(ByVal data As Object) As String
        If data Is DBNull.Value OrElse Convert.ToDecimal(data) = Nulls.NullDecimal Then
            Return "&nbsp;"
        Else
            Return Convert.ToDecimal(data).ToString(Config.DecimalFormat)
        End If
    End Function

    Public Shared Function GetNormalText(ByVal data As Object) As String
        If data Is DBNull.Value OrElse Convert.toString(data).Trim() = "" Then
            Return "&nbsp;"
        Else
            Return HttpUtility.HtmlEncode(Convert.toString(data))
        End If
    End Function

    Public Shared Function GetDateText(ByVal data As Object) As String
        If data Is DBNull.Value OrElse Convert.ToDateTime(data).Equals(Nulls.NullDate) Then
            Return "&nbsp;"
        Else
            Return Convert.ToDateTime(data).ToString(Config.DateFormat)
        End If
    End Function

    Public Shared Function CompareDate(ByVal date1 As DateTime, ByVal date2 As DateTime) As Integer
        date1 = New Date(date1.Year, date1.Month, date1.Day)
        date2 = New Date(date2.Year, date2.Month, date2.Day)
        Return DateTime.Compare(date1, date2)
    End Function
End Class
